#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time: 2018/1/3 下午5:17
@Author: CZC
@File: start_parse_task.py
"""
import time

import gevent
from gevent import monkey

from common import util
from common.logger import global_log
from common.queue_mq_thread import MqQueueThread
from config.beanstalk_config import aic_mq_conf
from parser.parse_gsxt_worker import ParseGsxtWorker

monkey.patch_all()


class ParserTask(object):
    def __init__(self):

        self.worker_list = {}
        self.config_list = {}
        self.log = global_log
        self.mq_none_count = 0

        # 初始化worker
        self.init_worker()

        self.count = 0  # count任务数目

        # 连接beanstalk
        self.aic_mq = MqQueueThread(server_conf=aic_mq_conf, log=self.log)
        self.aic_tube = self.aic_mq.output_tube

    def init_worker(self):
        self.worker_list = ParseGsxtWorker()
        self.log.info('初始化worker完成..')

    def start_worker(self):

        start_time = time.time()

        try:
            self.task_run()
        except Exception as e:
            self.log.error('周期任务异常!!!!')
            self.log.exception(e)

        end_time = time.time()
        self.log.info('扫描起始时间: {st}'.format(st=start_time))
        self.log.info('扫描结束时间: {et}'.format(et=end_time))
        self.log.info('扫描消耗时间: {t}s'.format(t=end_time - start_time))

        self.log.info('完成扫描... 退出程序')

    def task_run(self):

        self.task(self.worker_list)

        self.log.info("任务正在运行..")

        current_time = 0
        sleep_time = 300
        self.log.info('完成解析!!, 开始休眠休眠时间为: {rand}'.format(rand=sleep_time))

        while current_time < 300:
            time.sleep(1)
            current_time += 1

    # 单个省份运行任务
    def task(self, work_list):
        self.log.info("任务开始……")
        while True:
            try:
                company_data = self.__get_iterator_company_data()
            except:
                company_data = {}
                self.log.error('消息队列被关闭，等待中……')
                time.sleep(5)
            if company_data:
                self.mq_none_count = 0
                self.count += 1
                work_list.query_offline_task(company_data)
                self.log.info('当前任务进度: count = {count}'.format(count=self.count))
            else:
                self.mq_none_count += 1
                self.log.info('当前消息队列待解析为空，第 {} 次访问'.format(self.mq_none_count))
                if self.mq_none_count == 50:
                    self.log.info("当前消息队列暂时消费完成")
                    # break

    def __get_iterator_company_data(self):
        job = self.aic_mq.reserve_from_tubes(self.aic_tube)
        if job:
            body = job.body
            job.delete()
            company_data = util.json_loads(body)
            return company_data


def main():
    try:
        crawler = ParserTask()
        crawler.start_worker()
    except Exception as e:
        global_log.error("初始化错误:")
        global_log.exception(e)


if __name__ == "__main__":
    main()
